package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsUserAccountMapper;
import com.qf.mapper.DtsUserMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsRole;
import com.qf.pojo.DtsUser;
import com.qf.pojo.DtsUserAccount;
import com.qf.service.DtsUserService;
import com.qf.vo.DayStatis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author YDW
 * projectName:shop-manage
 * time:2022/5/19 11:24
 * description:
 */
@Service
public class DtsUserServiceImpl implements DtsUserService {

    @Autowired
    DtsUserMapper userMapper;
    @Autowired
    DtsUserAccountMapper userAccountMapper;

    @Autowired
    StatMapper statMapper;
    @Override
    public Integer getTotal() {
        Integer count = userMapper.selectCount(null);

        return count;
    }

    @Override
    public List<DayStatis> findUserStatis() {

        return statMapper.findUserStatis();
    }

    @Override
    public IPage<DtsUser> queryPage(Integer page, Integer limit, String sort, String order, String username, String mobile) {
        IPage<DtsUser> ipage=new Page<>(page,limit);
        QueryWrapper qw=new QueryWrapper<>();
        if(!StringUtils.isEmpty(username)){
            qw.like("username",username);
        }
        if(!StringUtils.isEmpty(mobile)){
            qw.like("mobile",mobile);
        }
        if(!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            if(order.equals("desc")){
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }
        userMapper.selectPage(ipage,qw);
        return ipage;
    }

    @Override
    public void updateapproveAgency(Integer userId, Integer settlementRate) {
        //更新用户详情表
        DtsUserAccount account=new DtsUserAccount();
        QueryWrapper<DtsUserAccount> qw1=new QueryWrapper<>();
        qw1.eq("user_id",userId);
        account.setSettlementRate(settlementRate);
        userAccountMapper.update(account,qw1);
        //更新用户状态
        DtsUser user=new DtsUser();
        user.setStatus(0);
        user.setUserLevel(2);
        QueryWrapper<DtsUser> qw2=new QueryWrapper<>();
        qw2.eq("id",userId);
        userMapper.update(user,qw2);
    }

    @Override
    public DtsUserAccount findUserAccountById(Integer id) {
        QueryWrapper<DtsUserAccount> qw=new QueryWrapper<>();
        qw.eq("id",id);
        DtsUserAccount account = userAccountMapper.selectOne(qw);
        return account;
    }
}
